<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

$GLOBALS['appID'] = "D03D1586EE6DF9540A1B7B86D8C229FA";
$GLOBALS['appKey'] = '860865420322249048';
$GLOBALS['url'] = 'https://area78-win.pospal.cn:443/';

function get_post($url, $data, $headers)
{
    $client = new Client();
    try {
        $response = $client->post($GLOBALS['url'] . $url, [
            'headers' => $headers,
            'json' => $data
        ]);
        // 输出请求结果
        return $response->getBody();
    } catch (RequestException $e) {
        // 输出请求异常
        return $e->getMessage();
    }
}

if (empty($_POST['number'])) {
    echo json_encode(array('code' => 1, 'msg' => '会员号不能为空'));
    exit;
}
if (empty($_POST['name'])) {
    echo json_encode(array('code' => 1, 'msg' => '名称不能为空'));
    exit;
}
if (empty($_POST['phone'])) {
    echo json_encode(array('code' => 1, 'msg' => '手机号不能为空'));
    exit;
}
if ($_POST['sex'] == '') {
    echo json_encode(array('code' => 1, 'msg' => '性别不能为空'));
    exit;
}
if (empty($_POST['birthday'])) {
    echo json_encode(array('code' => 1, 'msg' => '生日不能为空'));
    exit;
}
$number = $_POST['number'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$sex = $_POST['sex'];
$birthday = $_POST['birthday'];

$body = [
    'appId' => $GLOBALS['appID'],
    'customerInfo' => [
        'categoryName' => '普通',
        'number' => $number,
        'name' => $name,
        'phone' => $phone,
        // 'expiryDate' => '2099-12-31 23:59:59',
        'enable' => 1,
        'onAccount' => 0,
        'discount' => 95,
        'birthday' => $birthday,
        'extInfo' => [
            'sex' => $sex, //1-男，0-女
        ]
    ]
];
$headers = [
    'User-Agent' => 'openApi',
    'Content-Type' => 'application/json',
    'time-stamp' => time(),
    'data-signature' => strtoupper(md5($GLOBALS['appKey'] . json_encode($body)))
];

echo get_post('pospal-api2/openapi/v1/customerOpenApi/add', $body, $headers);
